/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function( $, common, ready, Collection, template, Model, dialog,datepicker ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'feedback',
                condition: {},
                model: {
                    url:_global.url.api + 'feedback',
                    event: {
                        '.item-remove:click': 'remove',
                        '.edit_listorder:change': 'setlistorder',
                        '.item-agree:click':'dealwith',
                        '.item-seenote:click':'seenote'
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    dealwith:function(e){
                        e.preventDefault();
                        _this.data = this.data;
                        var model = this;
                        var html =template('edit_tpl');
                        dialog({
                            'content': html,
                            ok: function() {
                                var reason = $.trim($("#reason").val());
                                if(reason==''){
                                    Alert({'msg':'请输入处理方案备注','type':'error'});
                                    return false;
                                }
                                _this.data.reason =reason;
                                _this.data.id =_this.data.id;
                                if(model.put()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    seenote:function(e){
                        e.preventDefault();
                        var html =template('seenote-tpl',this.data);
                        dialog({
                            'title':'意见反馈详细',
                            'content': html,
                            'width':400,
                            ok: function() {
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    setlistorder:function(e){
                        e.preventDefault();
                        var id = this.data.id;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if(new Model({
                                url: _global.url.api + 'article_set',
                                data:{id:id,listorder:listorder}
                            }).put()) {
                            _this.collection.fetch();
                        };
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm:ss'
                });
            });
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.condition.start_time = $('input[name="begintime"]').val();
                _this.collection.condition.end_time = $('input[name="endtime"]').val();
                _this.collection.reload();
            });
            $('#status').on('change', function(e) {
                e.preventDefault();
                _this.collection.condition.status = $(this).val();
                _this.collection.reload();
            });
            var click_search = function (fun) {
                _this.collection.condition.start_time =  $('input[name="begintime"]').val();
                _this.collection.condition.end_time = $('input[name="endtime"]').val();
                _this.collection.condition.status =$('#status').val();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.callback = function () {
                    if (fun && fun == 'toExcel') {
                        toExcel();
                        _this.collection.callback = function () {
                        }
                    }
                }
                _this.collection.reload();
            }
            var href;
            var toExcelWindow;
            $(".btn-excel").on("click", function (e) {
                e.preventDefault();
                href = $(this).attr('href');
                toExcelWindow = dialog({
                    content: '请稍等 ,正在导出...',
                    width: 200,
                    modal: true
                }).show();
                click_search('toExcel');
            });
            var toExcel = function () {
                toExcelWindow.close();
                console.log(_this.collection.result._count);
                var limit = 1000;
                var params = '';
                var data = _this.collection.condition;
                var total = _this.collection.result._count;

                if (total < 1) {
                    alert('没有需要导出的意见反馈');
                    return false;
                }

                for (var i in data) {
                    if (i == 'limit' || i == 'offset') continue;
                    params = params + '&' + i + '=' + encodeURIComponent(data[i]);
                }
                href = (href.indexOf('?') === -1) ? href + '?' : href;
                href = href + params + '&limit=' + limit + '&offset=';

                if (total > limit) {
                    var showData = {};
                    showData.pages = Math.ceil(total / limit);
                    showData.total = total;
                    showData.limit = limit;
                    showData.href = href;
                    dialog({
                        title: '导出意见反馈列表Excel文件',
                        content: template('to-excel-tpl', showData),
                        width: 600,
                        heigth: 500,
                        modal: true,
                        ok: function () {
                        },
                        cancel: function () {
                        }
                    }).show();
                } else {
                    window.location.href = href;
                }
            }
        }
    };

    new index();

});